<?php
namespace wepay\join\demo\common;
/* *
 * 银卡卡四要素授权类
 * 版本：0.1
 *
 */
class bankele
{
	
	
	//正式配置
	private $config = array(
		'merchantCode' => '300348', 	//商户号
		'accountId' => 'zhongqihui', 	//会员账户
		'accountType' => '2', 			//会员类型
		
		'getTokenUrl' => 'https://dc.jdpay.com/certification/bankelem/getToken.do', 		//授权接口路径
		'authenticationUrl' => 'https://dc.jdpay.com/certification/card/authentication.do', 	//获取验证码接口路径
		'confirmUrl' => 'https://dc.jdpay.com/certification/card/confirm.do', 				//提交验证码接口路径
		
		'key' => 'mgcB84Z84GkP9VaqTtRLtBrA', //秘钥
	);
	
	private $charset = 'UTF-8';
	
	private $version = '1.0';
	
	
	//授权
	public function getToken(){
		$data = array(
			'accountId' => $this->config["accountId"],
			'merchantCode' => $this->config["merchantCode"],
			'accountType' => $this->config["accountType"],
		);
		$token = '';
		$data = base64_encode(iconv('gb2312', 'utf-8', json_encode($data)));
		$checksign = $this->sha256($this->version.$this->charset.$data.$this->config['key']);
		
		$param = $this->pack(array(
			'charset' => $this->charset,
			'data' => $data,
			'version' => $this->version,
			'checkSign' => $checksign,
		));
		$result = array('errno' => 0,'errstr' => '','data' =>'');
		$resp = $this->post($param, $this->config['getTokenUrl']);
		//echo "==>".$resp;
		if(!empty($resp)){
			$respstr = $this->unpack($resp);
//			echo "-----------------------------------------------------------";
//			var_dump($respstr);
			if(!empty($respstr)){
				if(!empty($respstr['data']) && ($respstr['resultCode'] == 'SUCC')){
					$getdata = json_decode(base64_decode($respstr['data']), 'array');
					if(!empty($getdata) && ($getdata['resultCode'] == '000')){
						$token = $getdata['token'];
					}
				}
			} 
		} 
		return $token;
	}
	
	//获取验证码
	public function auth($realname, $idnumber, $cardpan, $bankcode, $mobile, $token){
		//借记卡数据

		$data = array(
			'realName' => $realname,	//真实姓名
			'idNumber' => $idnumber,	//身份证号
			'cardPan' => $cardpan,		//银行卡号
			'cardType' => 'D',			//卡借贷标识 信用卡C,借记卡D
			'bankCode' => $bankcode,	//发卡行编号 例如CMB，统一银行编码
			'mobile' => $mobile,		//签约手机号
			'cardExpireYear' => '',		//卡有效年 信用卡时必填，格式：YYYY
			'cardExpireMonth' => '', 	//卡有效月 信用卡时必填，格式：MM
			'cvs' => '',				//CVV2码 信用卡时必填
			'token' => $token,			//token值
		);
		$data = base64_encode(iconv('gb2312', 'utf-8', json_encode($data)));
		$checksign = $this->sha256($this->version.$this->charset.$data.$this->config['key']);
		$param = $this->pack(array(
			'charset' => $this->charset,
			'data' => $data,
			'version' => $this->version,
			'checkSign' => $checksign,
		));
		$result = array('error' => 0,'message' => '','data' =>'');
		$resp = $this->post($param, $this->config['authenticationUrl']);
		if(!empty($resp)){
			$respstr = $this->unpack($resp);
			if(!empty($respstr)){
				if(!empty($respstr['data']) && ($respstr['resultCode'] == 'SUCC')){
					$getdata = json_decode(base64_decode($respstr['data']), 'array');
					if(!empty($getdata) && ($getdata['resultCode'] == '000')){
						$result = array('error' => 0,'message' => $getdata['resultInfo'],'data' =>$getdata);
					} else {
						$result = array('error' => 1,'message' => $getdata['resultInfo'],'data' =>$getdata);
					}
				} else {
					$result = array('error' => 1,'message' => $respstr['resultInfo'], 'data' =>$respstr);
				}
			} else {
				$result = array('error' => 1,'message' => 'x1','data' =>$respstr);
			}
		}
		return $result;
	}
	
	//提交
	public function confirm($realname, $idnumber, $cardpan, $bankcode, $mobile, $token, $verfycode){

		//借记卡数据
		$data = array(
			'realName' => $realname,	//真实姓名
			'idNumber' => $idnumber,	//身份证号
			'cardPan' => $cardpan,		//银行卡号
			'cardType' => 'D',			//卡借贷标识 信用卡C,借记卡D
			'bankCode' => $bankcode,	//发卡行编号 例如CMB，统一银行编码
			'mobile' => $mobile,		//签约手机号			
			'cardExpireYear' => '',		//卡有效年 信用卡时必填，格式：YYYY
			'cardExpireMonth' => '', 	//卡有效月 信用卡时必填，格式：MM
			'cvs' => '',				//CVV2码 信用卡时必填
			'verfyCode' => $verfycode,	//验证码
			'token' => $token,			//token值
		);
		$data = base64_encode(iconv('gb2312', 'utf-8', json_encode($data)));
		$checksign = $this->sha256($this->version.$this->charset.$data.$this->config['key']);
		$param = $this->pack(array(
			'charset' => $this->charset,
			'data' => $data,
			'version' => $this->version,
			'checkSign' => $checksign,
		));

		$result = array('error' => 0,'message' => '','data' =>'');
		$resp = $this->post($param, $this->config['confirmUrl']);

		if(!empty($resp)){
			$respstr = $this->unpack($resp);
			if(!empty($respstr)){
				if(!empty($respstr['data']) && ($respstr['resultCode'] == 'SUCC')){

					$getdata = json_decode(base64_decode($respstr['data']), 'array');
					if(!empty($getdata) && ($getdata['resultCode'] == '000')){

						$result = array('error' => 0,'message' => $getdata['resultInfo'],'data' =>$getdata);
					} else {

						$result = array('error' => 1,'message' => $getdata['resultInfo'],'data' =>$getdata);
					}
				} else {

					$result = array('error' => 1,'message' => $respstr['resultInfo'], 'data' =>$respstr);
				}
			} else {
				$result = array('error' => 1,'message' => 'x1','data' =>$respstr);
			}
		}
		return $result;
	}
	
	private function sha256($text = ''){
		return hash("sha256", $text);
	}
	
	//以下两个方法，如果身份证简版认证类（idauth）中如何有需要可以将其放入 公共函数 的文件里面
	private function pack($data = array()){
		return urlencode(json_encode($data));
	}
	
	private function unpack($data = ''){
		return json_decode(urldecode($data), 'array');
	}
	
	private function post($param, $url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSLVERSION, 3);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);	//信任任何证书
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);	// 检查证书中是否设置域名,0不验证
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);		// 显示输出结果
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
			'Content-Type: application/json',                                                                                
			'Content-Length: ' . strlen($param))                                                                       
		); 
		$file_contents = curl_exec($ch);
		curl_close($ch);
		return $file_contents;
	}
	
}

//$bankeleobj = new bankele();
// $token=$bankeleobj->getToken();
// echo "token".$token;
// $result=$bankeleobj->auth('', '', '', '', '', '=');
// echo "auth------------------------------------------------------------";
// var_dump($respstr);
//$result=$bankeleobj->confirm('', '', '', '', '', '=','');
//var_dump($result);